﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XNASpelProjekt
{
    /// <summary>
    /// Base class for all game objects.
    /// </summary>
    public abstract class Actor
    {
        #region Member Variables

        //position and collision
        protected int x, y;                         //x,y-positionen
        public Rectangle Bounds{ get; set; }        //kollisionsrektangel
        public int X                                //X-property
        {
            get
            {
                return x;
            }
            set
            {
                x = value;
                Bounds = new Rectangle(x, Bounds.Y, Bounds.Width, Bounds.Height);
            }
        }
        public int Y                                //Y-property
        {
            get
            {
                return y;
            }
            set
            {
                y = value;
                Bounds = new Rectangle(Bounds.X, y, Bounds.Width, Bounds.Height);
            }
        }
        protected enum _CollisionType { collision, noCollision, death };

        //facing
        public enum _Facing { left, right }
        public _Facing Facing { get; set; }

        //graphics
        public Texture2D texture;
        public bool Hidden { get; set; }            //bestämmer om objektet ritas på skärmen eller ej

        #endregion

        #region protected methods

        /// <summary>
        /// Performs map-based collision detection
        /// </summary>
        /// <param name="collision">The actors collision rectangle</param>
        /// <returns>(Actor._CollisionType), the type of collision found</returns>
        protected virtual _CollisionType CollisionDetection(Rectangle collision)
        {
            foreach (Rectangle r in Game1.map.getMapDeathZone())
            {
                if (collision.Intersects(r))
                {
                    return _CollisionType.death;
                }
            }
            foreach (Rectangle r in Game1.map.getMapCollision())
            {
                if (collision.Intersects(r))
                {
                    return _CollisionType.collision;
                }
            }
            return _CollisionType.noCollision;            
        }

        #endregion

        #region public methods

        /// <summary>
        /// Updates the actor in the game
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public virtual void Update(GameTime gameTime) { }

        /// <summary>
        /// Draws the actor to the screen
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        /// <param name="spriteBatch">A SpriteBatch is used to draw textures.</param>
        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (!Hidden)
            {
                switch (Facing)
                {
                    case _Facing.left:
                        spriteBatch.Draw(texture, new Vector2(X, Y), Color.White);   
                        break;
                    case _Facing.right:
                        spriteBatch.Draw(texture, new Vector2(X, Y), texture.Bounds, Color.White, 0, new Vector2(0, 0), 1, SpriteEffects.FlipHorizontally, 0);  
                        
                        break;
                }               
            }

        }

        #endregion

        #region constructors
        private Actor() { }

        /// <summary>
        /// Base constructor
        /// Create an object with a texture, facing and position
        /// </summary>
        /// <param name="texture">The actors texture</param>
        /// <param name="x">(Optional) The actors x-position</param>
        /// <param name="y">(Optional) The actors y-position</param>
        /// <param name="facing">(Optional) The actors facing</param>
        /// <param name="hidden">(Optional) A hidden actor will not be drawn to screen</param>
        protected Actor(Texture2D texture, int x = 0, int y = 0, _Facing facing = _Facing.right, bool hidden = true)       //default facing då vissa objekt ej använder detta attribut
        {
            this.texture = texture;
            this.Bounds = texture.Bounds;
            this.X = x;
            this.Y = y;
            this.Facing = facing;
            this.Hidden = hidden;
        }

        #endregion
    }
}
